{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "![](https://pic2.ziyuan.wang/user/kohler/2025/09/1757917626387_06a025bb85c30.png)",
   "id": "8ddb18158f0416fb"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### Python正则表达式\n",
    "- 指定好匹配的模式 -pattern\n",
    "- 选择相应的方法-match,search等\n",
    "- 得到匹配的结果\n",
    "- re.match 尝试从字符串的起始位置匹配一个模式，如果不是起始位置匹配成功的话，match() 就返回 none。\n",
    "- re.search 扫描整个字符串并返回第一个成功的匹配。\n",
    "- re.findall 在字符串中找到正则表达式所匹配的所有子串，并返回一个列表，如果有多个匹配模式，则返回元组列表，如果没有找到匹配的，则返回空列表。"
   ],
   "id": "59813ff885d68082"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T06:41:57.157130Z",
     "start_time": "2025-09-15T06:41:57.154589Z"
    }
   },
   "cell_type": "code",
   "source": "input = '自然语言处理很重要。12abc789'",
   "id": "51ea215d80a6f5f7",
   "outputs": [],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T06:41:57.162211Z",
     "start_time": "2025-09-15T06:41:57.157130Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import re\n",
    "pattern = re.compile(r'.')\n",
    "re.findall(pattern, input)"
   ],
   "id": "87b2cee8cf75e4a1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['自',\n",
       " '然',\n",
       " '语',\n",
       " '言',\n",
       " '处',\n",
       " '理',\n",
       " '很',\n",
       " '重',\n",
       " '要',\n",
       " '。',\n",
       " '1',\n",
       " '2',\n",
       " 'a',\n",
       " 'b',\n",
       " 'c',\n",
       " '7',\n",
       " '8',\n",
       " '9']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### 字符集合\n",
    "- [abc] 指定包含字符\n",
    "- [a-zA-Z] 来指定所有英文字母的大小写\n",
    "- [^a-zA-Z] 指定不匹配所有英文字母"
   ],
   "id": "fc2bfd4858892d1d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T06:41:57.171616Z",
     "start_time": "2025-09-15T06:41:57.167041Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pattern = re.compile(r'[abc]')\n",
    "re.findall(pattern, input)"
   ],
   "id": "d1ff3aa684ef58bd",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'c']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T06:41:57.218182Z",
     "start_time": "2025-09-15T06:41:57.213219Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pattern = re.compile(r'[a-zA-Z]')\n",
    "re.findall(pattern, input)"
   ],
   "id": "7db2b63aaee40476",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'c']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T06:41:57.222809Z",
     "start_time": "2025-09-15T06:41:57.219276Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pattern = re.compile(r'[^a-zA-Z]')\n",
    "re.findall(pattern, input)"
   ],
   "id": "3eba0eee80b2f5a8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['自', '然', '语', '言', '处', '理', '很', '重', '要', '。', '1', '2', '7', '8', '9']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "**或方法**     \n",
    "将两个规则并列起来，以 '|' 连接，表示只要满足其中之一就可以匹配。\n",
    "- [a-zA-Z]|[0-9] 表示满足数字或字母就可以匹配，这个规则等价于 [a-zA-Z0-9]"
   ],
   "id": "5dc0f0b958ca40c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T06:42:26.994644Z",
     "start_time": "2025-09-15T06:42:26.989913Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pattern = re.compile(r'[a-zA-Z]|[0-9]')\n",
    "re.findall(pattern, input)"
   ],
   "id": "716426dde420aa2b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1', '2', 'a', 'b', 'c', '7', '8', '9']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### \\d\t匹配任意数字，等价于 [0-9].",
   "id": "b33b7ea58f7dd5ee"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T06:44:16.973959Z",
     "start_time": "2025-09-15T06:44:16.969640Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pattern = re.compile(r'\\d')\n",
    "re.findall(pattern, input)"
   ],
   "id": "917c37b78c77f24b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1', '2', '7', '8', '9']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### \\D\t匹配任意非数字",
   "id": "3162d9bcb669f85e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T06:44:44.878124Z",
     "start_time": "2025-09-15T06:44:44.873899Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pattern = re.compile(r'\\D')\n",
    "re.findall(pattern, input)"
   ],
   "id": "7068f3a014ab655",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['自', '然', '语', '言', '处', '理', '很', '重', '要', '。', 'a', 'b', 'c']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### \\w\t匹配字母数字及下划线",
   "id": "ad29c6dba29c8868"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T06:45:34.187174Z",
     "start_time": "2025-09-15T06:45:34.183099Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pattern = re.compile(r'\\w')\n",
    "re.findall(pattern, input)"
   ],
   "id": "8d89b5e8d16671cd",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['自',\n",
       " '然',\n",
       " '语',\n",
       " '言',\n",
       " '处',\n",
       " '理',\n",
       " '很',\n",
       " '重',\n",
       " '要',\n",
       " '1',\n",
       " '2',\n",
       " 'a',\n",
       " 'b',\n",
       " 'c',\n",
       " '7',\n",
       " '8',\n",
       " '9']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### \\W\t匹配非字母数字及下划线",
   "id": "daf91138e336fb6f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T06:46:04.251025Z",
     "start_time": "2025-09-15T06:46:04.245822Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pattern = re.compile(r'\\W')\n",
    "re.findall(pattern, input)"
   ],
   "id": "6fbad78f250b6269",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['。']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "**重复**   \n",
    "正则式可以匹配不定长的字符串   \n",
    "'*' 0 或多次匹配   \n",
    "'+' 1 次或多次匹配   \n",
    "'?' 0 或 1 次匹配  "
   ],
   "id": "da3a42345bfa0222"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T06:48:43.297452Z",
     "start_time": "2025-09-15T06:48:43.290548Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pattern = re.compile(r'\\d*')\n",
    "re.findall(pattern, input)"
   ],
   "id": "f474bb4ce63a71c6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['', '', '', '', '', '', '', '', '', '', '12', '', '', '', '789', '']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T06:49:18.860115Z",
     "start_time": "2025-09-15T06:49:18.855083Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pattern = re.compile(r'\\d+')\n",
    "re.findall(pattern, input)"
   ],
   "id": "75a9ea029476f47d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['12', '789']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T06:50:00.384776Z",
     "start_time": "2025-09-15T06:50:00.380626Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pattern = re.compile(r'\\d?')\n",
    "re.findall(pattern, input)"
   ],
   "id": "f58c1cf2876a3257",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['',\n",
       " '',\n",
       " '',\n",
       " '',\n",
       " '',\n",
       " '',\n",
       " '',\n",
       " '',\n",
       " '',\n",
       " '',\n",
       " '1',\n",
       " '2',\n",
       " '',\n",
       " '',\n",
       " '',\n",
       " '7',\n",
       " '8',\n",
       " '9',\n",
       " '']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "**精确匹配和最小匹配**   \n",
    "'{m}' 精确匹配 m 次   "
   ],
   "id": "5b93d239718c5e6f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T07:00:35.577396Z",
     "start_time": "2025-09-15T07:00:35.572831Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pattern = re.compile(r'\\d{3}')\n",
    "re.findall(pattern, input)"
   ],
   "id": "4e6368c8a300bb0b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['789']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "'{m,n}' 匹配最少 m 次，最多 n 次。(n>m)  ",
   "id": "17497d5d54222bfc"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T07:01:15.000668Z",
     "start_time": "2025-09-15T07:01:14.995874Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pattern = re.compile(r'\\d{1,3}')\n",
    "re.findall(pattern, input)"
   ],
   "id": "a36cc357688ec9a5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['12', '789']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### match 与 search \n",
    "它们的返回不是一个简单的字符串列表，而是一个 MatchObject，可以得到更多的信息。    \n",
    "如果匹配不成功，它们则返回一个 NoneType。所以在对匹配完的结果进行操作之前，必需先判断一下是否匹配成功了。   \n",
    "match 从字符串的开头开始匹配，如果开头位置没有匹配成功，就算失败了；而 search 会跳过开头，继续向后寻找是否有匹配的字符串。   "
   ],
   "id": "b6996ddc101504d8"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T07:03:46.231215Z",
     "start_time": "2025-09-15T07:03:46.226834Z"
    }
   },
   "cell_type": "code",
   "source": "input2 = '123自然语言处理'",
   "id": "832832155c63c496",
   "outputs": [],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T07:04:26.059234Z",
     "start_time": "2025-09-15T07:04:26.054304Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pattern = re.compile(r'\\d')\n",
    "match = re.match(pattern, input2)\n",
    "match.group()"
   ],
   "id": "1ad53b4d4e05e596",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### 字符串的替换和修改\n",
    "在目标字符串中按规则查找匹配的字符串，再把它们替换成指定的字符串。你可以指定一个最多替换次数，否则将替换所有的匹配到的字符串。  \n",
    "sub (rule, replace, target [,count] )  \n",
    "subn (rule, replace, target [,count] )  \n",
    "第一个参数是正则规则，第二个参数是指定的用来替换的字符串，第三个参数是目标字符串，第四个参数是最多替换次数。  \n",
    "sub 返回一个被替换的字符串  \n",
    "subn 返回一个元组，第一个元素是被替换的字符串，第二个元素是一个数字，表明产生了多少次替换。  "
   ],
   "id": "45bb511a079e36c5"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T07:06:54.373753Z",
     "start_time": "2025-09-15T07:06:54.369288Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pattern = re.compile(r'\\d')\n",
    "re.sub(pattern, r'数字', input2)#对数字进行替换"
   ],
   "id": "ec3ba5c6ad00471f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'数字数字数字自然语言处理'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T07:07:46.929975Z",
     "start_time": "2025-09-15T07:07:46.923223Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pattern = re.compile(r'\\d')\n",
    "re.subn(pattern, r'数字', input2)#对数字进行替换,并给出次数"
   ],
   "id": "50c9217039d2cf80",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('数字数字数字自然语言处理', 3)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "split 切片函数。使用指定的正则规则在目标字符串中查找匹配的字符串，用它们作为分界，把字符串切片。  \n",
    "split (rule, target [,maxsplit] )  \n",
    "第一个参数是正则规则，第二个参数是目标字符串，第三个参数是最多切片次数，返回一个被切完的子字符串的列表  "
   ],
   "id": "54fc18665014598e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T07:10:48.392434Z",
     "start_time": "2025-09-15T07:10:48.388222Z"
    }
   },
   "cell_type": "code",
   "source": [
    "input3  = '自然语言处理123机器学习456深度学习'\n",
    "pattern = re.compile(r'\\d+')\n",
    "re.split(pattern, input3)"
   ],
   "id": "3df57c53c6a363ab",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['自然语言处理', '机器学习', '深度学习']"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "'(?P<...>)' 命名组  \n",
    "<...> 里面是你给这个组起的名字，"
   ],
   "id": "f315c7329da538d1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T07:13:52.205629Z",
     "start_time": "2025-09-15T07:13:52.201734Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pattern = re.compile(r'(?P<dota>\\d+)(?P<lol>\\D+)')\n",
    "m = re.search(pattern, input3)\n",
    "m.group('dota')"
   ],
   "id": "a268df2ccda7a71f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'123'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T07:15:25.126292Z",
     "start_time": "2025-09-15T07:15:25.122556Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 例子\n",
    "input = 'number 338-343-220'  \n",
    "pattern = re.compile(r'(\\d\\d\\d-\\d\\d\\d-\\d\\d\\d)')\n",
    "m = re.search(pattern, input)\n",
    "print(m.groups())"
   ],
   "id": "466fb47d802d02df",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('338-343-220',)\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "21d8b60d1a2eaa2"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
